package com.inwavetech.os.sync.jsonparser;

import com.inwavetech.os.entity.Os;
import com.inwavetech.os.entity.enummap.OsClosingStatusMap;
import com.inwavetech.os.entity.enummap.OsServiceOrderTypeMap;
import com.inwavetech.os.entity.enummap.OsStatusMap;
import com.inwavetech.os.util.SyncDateFormat;
import com.inwavetech.os.util.field.BigDecimalField;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by jgskin on 3/5/14.
 */
public class OsJsonParser extends BaseJsonParser {

    public OsJsonParser(
            OsStatusMap statusMap,
            OsClosingStatusMap closingStatusMap,
            OsServiceOrderTypeMap serviceOrderTypeMap,
            SyncDateFormat syncDateFormatter) {
        super();

        expectedKeys.add("client");
        expectedKeys.add("store");
        expectedKeys.add("opener");
        expectedKeys.add("origin");
        expectedKeys.add("contact");
        expectedKeys.add("authorization");
        expectedKeys.add("activated_user");
        expectedKeys.add("finished_user");
        expectedKeys.add("assignment");
        expectedKeys.add("assignment_time");
        expectedKeys.add("maintenance_observation");
        expectedKeys.add("service_justification");
        expectedKeys.add("travel_justification");
        expectedKeys.add("roundup");
        expectedKeys.add("future_observation");
        expectedKeys.add("name_responsible");
        expectedKeys.add("travel_price");
        expectedKeys.add("service_price");
        expectedKeys.add("service_order_type_id");
        expectedKeys.add("status_id");
        expectedKeys.add("closing_status_id");
        expectedKeys.add("created_at");
        expectedKeys.add("activated_at");
        expectedKeys.add("assignment_date");
        expectedKeys.add("updated_at");
        expectedKeys.add("finished_at");

        JsonCleaner bigDecimalCleaner = new JsonCleaner() {
            @Override
            public Object validateAndClean(String receivedValue) {
                if (receivedValue.trim().isEmpty()) {
                    return null;
                }

                try {
                    return new BigDecimal(receivedValue);
                } catch (NumberFormatException e) {
                    return false;
                }
            }
        };

        cleaners.put("travel_price", bigDecimalCleaner);
        cleaners.put("service_price", bigDecimalCleaner);

        cleaners.put("status_id", new EnumCleaner(statusMap, true));
        cleaners.put("closing_status_id", new EnumCleaner(closingStatusMap, false));
        cleaners.put("service_order_type_id", new EnumCleaner(serviceOrderTypeMap, true));

        cleaners.put("created_at", new DateCleaner(syncDateFormatter, true));
        cleaners.put("updated_at", new DateCleaner(syncDateFormatter, true));
        cleaners.put("activated_at", new DateCleaner(syncDateFormatter, false));
        cleaners.put("assignment_date", new DateCleaner(syncDateFormatter, false));
        cleaners.put("finished_at", new DateCleaner(syncDateFormatter, false));
    }

    @Override
    protected void populate(HashMap<String, Object> cleanedValues, Object anObject) {
        Os os = (Os) anObject;

        // simple values
        os.original_id = (String) cleanedValues.get("id");
        os.client = (String) cleanedValues.get("client");
        os.store = (String) cleanedValues.get("store");
        os.opener = (String) cleanedValues.get("opener");
        os.origin = (String) cleanedValues.get("origin");
        os.contact = (String) cleanedValues.get("contact");
        os.authorization = (String) cleanedValues.get("authorization");
        os.activated_user = (String) cleanedValues.get("activated_user");
        os.finished_user = (String) cleanedValues.get("finished_user");
        os.assignment = (String) cleanedValues.get("assignment");
        os.assignment_time = (String) cleanedValues.get("assignment_time");
        os.maintenance_observation = (String) cleanedValues.get("maintenance_observation");
        os.service_justification = (String) cleanedValues.get("service_justification");
        os.travel_justification = (String) cleanedValues.get("travel_justification");
        os.roundup = (String) cleanedValues.get("roundup");
        os.future_observation = (String) cleanedValues.get("future_observation");
        os.name_responsible = (String) cleanedValues.get("name_responsible");

        // parsed values
        os.travel_price = (BigDecimal) cleanedValues.get("travel_price");
        os.service_price = (BigDecimal) cleanedValues.get("service_price");

        os.status = (Os.Status) cleanedValues.get("status_id");
        os.closing_status = (Os.ClosingStatus) cleanedValues.get("closing_status_id");
        os.service_order_type = (Os.ServiceOrderType) cleanedValues.get("service_order_type_id");

        os.created_at = (Date) cleanedValues.get("created_at");
        os.activated_date = (Date) cleanedValues.get("activated_at");
        os.assignment_date = (Date) cleanedValues.get("assignment_date");
        os.updated_at = (Date) cleanedValues.get("updated_at");
        os.finished_at = (Date) cleanedValues.get("finished_at");
    }
}
